package com.crazymakercircle.dhptester.benchmark;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crazymakercircle.dhptester.client.DhpTesterNettyClient;
import com.crazymakercircle.entity.TestResultData;
import com.crazymakercircle.util.FileHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.concurrent.atomic.LongAdder;

import static com.crazymakercircle.constants.ServerConstants.DF;
import static com.crazymakercircle.constants.ServerConstants.SDF;

public class HttpBenchmark
{
    public long test_count = 10000;//运行次数
    public long start_time = 0;
    public volatile long end_time = 0;
    public volatile int thread_number = 64;
    public LongAdder total_req = new LongAdder();
    public LongAdder total_req_res = new LongAdder();
    public LongAdder total_err = new LongAdder();
    public LongAdder below_10 = new LongAdder();
    public LongAdder between_10_20 = new LongAdder();
    public LongAdder between_20_30 = new LongAdder();
    public LongAdder over_30 = new LongAdder();
    public LongAdder total_res_time = new LongAdder();


    public volatile boolean isOver;

    public HttpBenchmark()
    {
    }



    public TestResultData runJsonTestCase(String content)
    {
        Logger logger = LogManager.getLogger(DhpTesterNettyClient.class.getName());
        logger.info("httpbenchmark start");

        String host = "127.0.0.1";
        int port = 80;
        int client_num = 100;
        JSONObject jsondata = null;

        if (StringUtils.isNotBlank(content))
        {
            jsondata = JSON.parseObject(content);
            client_num = Integer.parseInt(jsondata.getString("client_num"));
            this.test_count = Integer.parseInt(jsondata.getString("test_count"));
            host = jsondata.getString("host");
            port = Integer.parseInt(jsondata.getString("port"));
        }

        return runTestTask(logger, host, port, client_num, jsondata);
    }

    public TestResultData runTestTask(Logger logger, String host, int port, int client_num, JSONObject jsondata)
    {
        try
        {
            logger.info("httpbenchmark qps stat timer start");
            DhpTesterNettyClient client = new DhpTesterNettyClient(host, port, client_num, jsondata, this);
            client.start();
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            Date dates = new Date(start_time);
            Date datee = new Date(end_time);
            String st = SDF.format(dates);
            String et = SDF.format(datee);

            long take_time = (end_time - start_time) / (1000);
            int tn = thread_number;
            long total_req_value = total_req.longValue();
            long total_res_value = total_req_res.longValue();
            long total_err_value = total_err.longValue();
            long total_res_time_value = total_res_time.longValue();
            long below_10_value = below_10.longValue();
            long between_10_20_value = between_10_20.longValue();
            long between_20_30_value = between_20_30.longValue();
            long over_30_value = over_30.longValue();
            if(take_time<=0)
            {
                take_time=1;
            }
            String qps = DF.format((total_req_value / take_time));
            String avg_latency;
            if (total_res_value == 0)
            {
                avg_latency = "0";
            } else
            {
                avg_latency = DF.format((double) total_res_time_value / (double) total_req_value);
            }

            try
            {

                TestResultData testResultData = new TestResultData(st, et, tn, client_num, test_count,
                        total_req_value, total_res_value, total_err_value, qps, avg_latency, below_10_value, between_10_20_value, between_20_30_value, over_30_value);
                testResultData.setTakeTime(take_time);
                testResultData.report();
                logger.info("httpbenchmark mail sending success");

                System.gc();
                return testResultData;
            } catch (Exception e2)
            {
                logger.error(e2.toString());
                return null;
            } finally
            {
                logger.info("httpbenchmark complete");
            }
        }

    }


}
